package com.icez.bottom_navigation

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding

/**
 * 底部Navigation控件,用于替换google的
 * @sample
 * @author Icez
 */
class BottomNavigationView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : ViewGroup(context, attrs, defStyleAttr) {

    // 适配器
    private var mBottomNavigationAdapter:BottomNavigationAdapter<ViewDataBinding> ?= null

    // 子控件的宽度
    private var mChildWidth = 0

    // 子控件的binding
    private var mBindingLists:ArrayList<ViewDataBinding> = arrayListOf()


    /**
     * 设置适配
     * @sample
     * @param mBottomNavigationAdapter 适配器
     * @author Icez
     */
    fun <T:ViewDataBinding> setBottomNavigationAdapter(mBottomNavigationAdapter: BottomNavigationAdapter<T>){
        // 删除所有子控件
        this.removeAllViews()
        // 清除子控件数据
        mBindingLists.clear()
        // 设置上下文
        mBottomNavigationAdapter.mContext = context
        // 获取LayoutInflater
        val layoutInflater = LayoutInflater.from(context)
        // 遍历添加子控件
        for(i in 0 until mBottomNavigationAdapter.getSize()){
            // 获取子控件的binding
            val binding = DataBindingUtil.inflate<T>(layoutInflater,mBottomNavigationAdapter.getItemView(i),null,false)
            // 添加子控件的binding到集合中去
            mBindingLists.add(binding)
            // 获取子控件
            val child = binding?.root
            // 设置子控件点击
            child?.setOnClickListener {
                // 遍历子控件并且设置选中的和没有选中的方法
                for (j in 0 until mBottomNavigationAdapter.getSize()){
                    if(i==j){
                        // 设置选中的方法
                        mBottomNavigationAdapter.selectedStatus(mBindingLists.get(i) as T,i)
                    }else{
                        // 设置未选中的方法
                        mBottomNavigationAdapter.normalStatus(mBindingLists.get(j) as T,j,i)
                    }
                }
            }
            // 添加子控件到布局中
            addView(child)
            // 初始化数据
            mBottomNavigationAdapter.initViewValue(mBindingLists.get(i) as T,i)
            // 默认的选中的索引
            var defalutSelectPosition = 0
            // 判断默认的选中索引是否大于最大值
            if(mBottomNavigationAdapter.setDefaultSelectPosition()<mBottomNavigationAdapter.getSize()){
                // 设置默认选中的索引
                defalutSelectPosition = mBottomNavigationAdapter.setDefaultSelectPosition()
            }
            if(i==defalutSelectPosition){
                // 设置选中的方法
                mBottomNavigationAdapter.selectedStatus(mBindingLists.get(i) as T,i)
            }else{
                // 设置未选中的方法
                mBottomNavigationAdapter.normalStatus(mBindingLists.get(i) as T,i,defalutSelectPosition)
            }
        }
        // 重新绘制计算布局
        invalidate()
    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        // 显示的子控件数量
        var visableChildCount = 0
        if(childCount>0){
            // 遍历子控件
            for(i in 0 until childCount){
                // 子控件是否显示
                if(getChildAt(i).isVisible){
                    // 设置子控件显示的数量
                    visableChildCount++
                }
            }
        }
        if(visableChildCount == 0){
            // 计算布局的大小
            super.onMeasure(widthMeasureSpec, heightMeasureSpec)
            return
        }
        // 计算出子控件的宽度Spec
        val childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec) / visableChildCount, MeasureSpec.EXACTLY)
        // 计算子控件的高度Spec
        val childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(heightMeasureSpec), MeasureSpec.EXACTLY)

        if(childCount>0){
            // 遍历子控件
            for(i in 0 until childCount){
                val child = getChildAt(i)
                if(!child.isVisible){
                    continue
                }
                // 计算子控件的大小
                child.measure(childWidthMeasureSpec,childHeightMeasureSpec)
                mChildWidth = child.measuredWidth
            }
        }
        // 计算布局的大小
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        // 获取布局的高度
        val height = bottom - top
        if(childCount>0){
            // 遍历子控件
            for(i in 0 until childCount){
                // 获取子控件
                val child = getChildAt(i)
                if(!child.isVisible){
                    continue
                }
                // 给子控件布局
                child.layout(i*mChildWidth,0,(i+1)*mChildWidth,height)
            }
        }
    }

}